package com.ydd.service.impl;

import com.ydd.service.CommonService;
import com.ydd.utils.AliOssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Service
public class CommonServiceImpl implements CommonService {

    @Autowired
    private AliOssUtil aliOssUtil;
    /**
     * 图片上传
     *
     * @param file
     * @return
     */
    @Override
    public String upload(MultipartFile file) throws IOException {
        //原始文件名
        String originalFilename = file.getOriginalFilename();
        //截取原始文件名的后缀   dfdfdf.png
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        //构造新文件名称
        String objectName = UUID.randomUUID().toString() + extension;

        //本地存储
     /*   String url = "/upload/"+objectName;
        File file1 = new File("D:\\imgs\\"+objectName);
        file.transferTo(file1);*/

        //远程存储
        //文件的请求路径
        String url = aliOssUtil.upload(file.getBytes(), objectName);

        //文件的请求路径
        return "/upload/"+objectName;
    }
}
